﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Whatzup.Mobi.Common.Model;
using Whatzup.Mobi.Common;
using Whatzup.Mobi.AppSvc.Provider;

namespace Whatzup.Mobi.AppSvc
{
    /// <summary>
    /// Enum that specify the format of the stream
    /// </summary>
    public enum MediaType
    {
        acc,
        pls
    }

    /// <summary>
    /// The main class for supplying and retrieving the data.
    /// </summary>
    public class MediaRepo : IMediaRepo
    {
        IProvider m_provider;
        public MediaRepo(IServiceLocator serviceLocator)
        {
            m_provider = serviceLocator.GetService<IMediaProvider>();
        }

        /// <summary>
        /// Blackberry /mobile phone (aacplus format)
        /// </summary>
        /// <returns></returns>
        public CompositeMediaType GetMobileUrl()
        {
            Uri uri;
            string uriString = m_provider.Retrieve()[UrlType.mobile];
            bool result = Uri.TryCreate(uriString, UriKind.Absolute, out uri);

            return CreaterCompositeMediaType(uri,MediaType.acc);
        }
        
        /// <summary>
        /// PC Link
        /// </summary>
        /// <returns></returns>
        public CompositeMediaType GetPcMediaUrl()
        {
            Uri uri;
            string uriString = m_provider.Retrieve()[UrlType.pc];
            bool result = Uri.TryCreate(uriString, UriKind.Absolute, out uri);

            return CreaterCompositeMediaType(uri,MediaType.pls);
        }

        /// <summary>
        /// Converts the uri and media type to CompositeMediaType model.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="mediaType"></param>
        /// <returns></returns>
        private CompositeMediaType CreaterCompositeMediaType(Uri uri, MediaType mediaType)
        {
            if (uri == null)
                throw new ArgumentNullException("Uri");

            return new CompositeMediaType
            {
                Url = uri.AbsoluteUri,
                FormatType = mediaType.ToString()
            };
        }
    }
}